/*
 * @FileName: task.c
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-08-08 16:34:29
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-11-10 09:39:03
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */

#include "easy_timer.h"
#include "app_fsm.h"
#include "app_fault.h"
#include "app_mppt.h"
#include "app_analog.h"
#include "loop.h"
#include "dev_info.h"
#include "log.h"
#include "bsp.h"
#include "mb_rtu_m.h"
#include "led.h"
#include "mb_reg.h"


EASY_TIMER_DEF(timer_1ms);
EASY_TIMER_DEF(timer_10ms);
EASY_TIMER_DEF(timer_500ms);
EASY_TIMER_DEF(timer_1000ms);


// 1ms任务
static void task_1ms(void)
{
    mbTimeInc();
    powerMeterCaculate(&Vrms);
    app_fault_run();
}

// 10ms任务
static void task_100ms(void)
{

}

static void task_500ms(void)
{
    bsp_uart_clear_error();
    mbReadHoldReg(DSP_ADDR, 0, MSG_SIZE); // 读普通寄存器

    if (dspReg[3].u16 == 8888 && app_fault_get_bits() == 0) { // DSP通知闭合继电器
        bsp_gpio_set(PORT_RELAY_L, PIN_RELAY_L);
        bsp_gpio_set(PORT_RELAY_N, PIN_RELAY_N);
    } else {  // DSP通知断开继电器
        bsp_gpio_reset(PORT_RELAY_L, PIN_RELAY_L);
        bsp_gpio_reset(PORT_RELAY_N, PIN_RELAY_N);
    }
}

static void task_1000ms(void)
{

} 

void task_init(void)
{
    app_analog_init();
    // 1ms任务
    easy_timer_create(timer_1ms);
    easy_timer_start(timer_1ms, 1, EASY_TIMER_MODE_REPEATED, task_1ms);

    // 10ms任务
    easy_timer_create(timer_10ms);
    easy_timer_start(timer_10ms, 100, EASY_TIMER_MODE_REPEATED, task_100ms);

        // 25ms任务
    easy_timer_create(timer_500ms);
    easy_timer_start(timer_500ms, 500, EASY_TIMER_MODE_REPEATED, task_500ms);

    // 1000ms任务
    easy_timer_create(timer_1000ms);
    easy_timer_start(timer_1000ms, 1000, EASY_TIMER_MODE_REPEATED, task_1000ms);

}
